<%@ page pageEncoding="UTF-8"%>
<%@ include file="../commons/head.jsp"%>
<title>合同强制中了</title>
<style type="text/css">
	.button{width: 100px;height: 30px;margin:3px 20px 3px 0;padding-top: 3px;}
	*{font-size: 14px;}
	.th td{font-weight: 700;}
	/* label{margin: 0 10px 0 18px;font-weight: 500;height: 25px;line-height: 25px;} */
     .control-label{padding: 0;font-weight: bold;}
     .diaform{margin: 20px auto;}
     h3{border-bottom:1px solid #ddd;padding-bottom:5px;width: 98%;  }
     .datagrid-view{border-top:1px solid #ddd;margin-top: 8px;}
     .datagrid-pager{border-bottom:1px solid #ccc;}
     hr{margin:15px 0 13px 0 ;background-color: #ddd;height: 1px;border: 0; width: 120%;}
     .jianju{margin-left:15px; }
     .datagrid-htable{
     font-weight:bold;
	/* background-color: #148CCA; */
	}
	.demo_line_01{
    
    margin: 5px 0;
    font-size: 50px;
    line-height: 1px;
    text-align: center;
	}
	.demo_line_01 .line{
	display:inline-block;
	   width:47.8%;
	   height:1px;
	   background:#ddd;
	   vertical-align:middle;
	   margin-top:8px;
	}
	.container-fluid{
	padding-left: 15px;
	}
	.datagrid-ftable td div{
		font-size: larger;
	
	}
	#pulldown{
		margin-top: 20px;
	}
	
/* td.datagrid-header-over{
	background: #148CCA !important;
} */

.datagrid-ftable {
	color: red;
	font-weight: bold;
}
.datagrid-body {
height:300px !important;
}
.datagrid-view {
height:360px !important;
}


</style>

<script type="text/javascript">
var queryInit = true;
var c;
var d;
	
$(function(){
	function scrollShow(datagrid) {
	    datagrid.prev(".datagrid-view2").children(".datagrid-body").html("<div style='width:" + datagrid.prev(".datagrid-view2").find(".datagrid-header-row").width() + "px;border:solid 0px;height:1px;'></div>");
	};
	scrollShow($("#cc"));
	
	var params = {
		id:$("#hiddenid").val(),
	};
	$.ajax({
		url:"${ctx}/contractterminate/getcustomer",
		data:params,
		success:function(data){debugger
			queryInit = false;
			$("#phone").textbox("setValue", data.customerPhone);
			$("#customerName").textbox("setValue", data.customerName);
			$("#principal").textbox("setValue", data.approvalAmount);
			
			var se = parseFloat(data.serFee);
			if(se/100 != "" && se/100 != null && !isNaN(se/100)){
				$("#serFee").textbox("setValue", se/100);
			}else{
				$("#serFee").textbox("setValue", 0);
			}
			
			$("#appDate").textbox("setValue", data.validDate);
			$("#cyc").textbox("setValue", data.approvalCyc);
			
			if(data.forceAmount != "" && data.forceAmount != null && !isNaN(data.forceAmount)){
				$("#forceAmount").val(data.forceAmount);
			}else{
				$("#forceAmount").val(0);
			}
			
			$("#cc").datagrid({url:"${ctx}/contractterminate/list",queryParams:params});
			
			$.ajax({
				url:"${ctx}/contractterminate/findMax",
				data:params,
				success:function(data){
					if(data.totalPrin != "" && data.totalPrin != null){
						$("#totalPrin").val(data.totalPrin);
					}else{
						$("#totalPrin").val(0);
					}
					if(data.totalInterest != "" && data.totalInterest != null){
						$("#totalInterest").val(data.totalInterest);
					}else{
						$("#totalInterest").val(0);
					}
					
					$.ajax({
						url:"${ctx}/contractterminate/findSUM",
						data:{"contractbaseid":$("#hiddenid").val()},
						success:function(data){debugger
							
							if(data != "" && data != null){
								$("#delayAmountSum").val(data);
							}else{
								$("#delayAmountSum").val(0);
							}
							
							countSum();
						}
					});
				}
			});
			
		}
	});
	$("#phone").textbox({
        inputEvents: $.extend({},$.fn.textbox.defaults.inputEvents,{
            click:function(event){
            	if(queryInit == false && $("#phone").val() != ""){
        			window.location.href="${pageContext.request.contextPath}/Boxuser/boxuserinfo?userId="+$("#phone").val();
        		}
            }   
	 })
	});
});	
function countSum(){debugger
	var a;
	var b;
	var e;
	var f;
	if($("#totalPrin").val() != "" && $("#totalPrin").val() != null){
		a = parseFloat($("#totalPrin").val());
	}else{
		a=0;
	}
	if($("#totalInterest").val() != "" && $("#totalInterest").val() != null){
		b = parseFloat($("#totalInterest").val());
	}else{
		b=0;
	}
	if($("#delayAmountSum").val() != "" && $("#delayAmountSum").val() != null){
		c = parseFloat($("#delayAmountSum").val());
	}else{
		c=0;
	}
	if($("#forceAmount").val() != "" && $("#forceAmount").val() != null){
		d = parseFloat($("#forceAmount").val());
		d = d.toFixed(2);
	}else{
		d=0;
	}
	$("#totalSum").val((a+b+c+parseFloat(d)));
	if($("#totalSum").val() != "" && $("#totalSum").val() != null){
		e = parseFloat($("#totalSum").val());
	}else{
		e=0;
	}
	if($("#principal").val() != "" && $("#principal").val() != null){
		f = parseFloat($("#principal").val());
	}else{
		f=0;
	}
	$("#income").val(e-f);
}
function saved(){debugger
	if(isNaN($("#forceAmount").val())){
		alert("强制结束收款金额项必须填写数字！");
	}else{
		var principal;
		var interest;
		$.ajax({
			url:"${ctx}/contractterminate/list1",
			data:{"id":$("#hiddenid").val()},
			success:function(data){debugger
				if(data != null && data.length >0){
					principal = data[0].principal;
					interest = data[0].interest;
					
					if($("#delayAmountSum").val() != "" && $("#delayAmountSum").val() != null){
						c = parseFloat($("#delayAmountSum").val());
					}else{
						c=0;
					}
					if($("#forceAmount").val() != "" && $("#forceAmount").val() != null){
						d = parseFloat($("#forceAmount").val());
						d = d.toFixed(2);
					}else{
						d=0;
					}
					if(principal != "" && principal != null){
						principal = parseFloat(principal);
					}else{
						principal=0;
					}
					if(interest != "" && interest != null){
						interest = parseFloat(interest);
					}else{
						interest=0;
					}
					var totalAmount = principal+interest+c+parseFloat(d);
					$.ajax({
						url:"${ctx}/contractterminate/save",
						data:{"id":$("#hiddenid").val(),"forceAmount":$("#forceAmount").val(),"totalAmount":totalAmount},
						success:function(data){
							if(data.result == 1){
								alert("保存成功！");
							}else{
								alert("保存失败！");
							}
						}
					});
				}
			}
		});
		
	}
}





function reload(closeFlag){
	window.location.href='${pageContext.request.contextPath}/contract/search';
} 

 function closeDlg(){
	$("#cc").datagrid('reload');
	$('#dlg').dialog('close');
}; 

function rowStyle(index,row){
	if (index%2==0){     
        return 'background-color:lightcyan;';     
    }     
}

$(function(){
    $("#phone").textbox('textbox').bind('keyup', function(e){
	        $("#phone").textbox('setValue', $(this).val().replace(/\D/g,''));
    });
});

function formatMoney(number) {
number = Math.round(number * 100) / 100;
return outputDollars(Math.floor(number - 0) + '') + outputCents(number - 0);
}
function outputDollars(number) {
if (number.length <= 3)
	return (number == '' ? '0' : number);
else {
	var mod = number.length % 3;
	var output = (mod == 0 ? '' : (number.substring(0, mod)));
	for (i = 0; i < Math.floor(number.length / 3); i++) {
		if ((mod == 0) && (i == 0))
			output += number.substring(mod + 3 * i, mod + 3 * i + 3);
		else
			output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
	}
	return (output);
}
}
function outputCents(amount) {
amount = Math.round(((amount) - Math.floor(amount)) * 100);

if (amount == 10 || amount == 20 || amount == 30 || amount == 40
		|| amount == 50 || amount == 60 || amount == 70 || amount == 80
		|| amount == 90) {
	amount = (amount.toString()).substr(0, (amount.toString()).length - 1);
	return '.' + amount;
} else if (amount == 0) {
	return "";
} else if (amount == 1 || amount == 2 || amount == 3 || amount == 4
		|| amount == 5 || amount == 6 || amount == 7 || amount == 8
		|| amount == 9) {
	return '.0' + amount;
} else {
	return '.' + amount;
}

}
//鼠标悬停显示提示
function showTips(value, row, index) {
	var content = '';
	if(value != null && value != ''){
	    if(value.length>6 && strDateTime(value)){ //如果为时间
			value = value.substr(0,10);    	
	    }
	    content = '<span title="' + value + '" class="note" >' + value + '</span>';
	}
    return content;
}
function showMoneyTips(value, row, index) {
	var m = formatMoney(value,row,index);
    return showTips(m);
}
//判断是否为时间格式 yyyy-MM-dd HH:mm:ss
function strDateTime(str){
	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
	var r = str.match(reg); 
	if(r==null)return false; 
	var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 
	return (d.getFullYear()==r[1]&&
	(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7
	]);
}



</script>
</head>
<body style="height: 200%">
<div class="jianju" style="height: 1000px;">
<h3>合同强制中了</h3>

<div id="tb" style="padding:5px;height:auto">
<hr/>
	<div>
	<input value="${id}" id="hiddenid" hidden="hidden"/>
	  <label style="margin-left:18px">手机号码：</label><input id="phone" class="easyui-textbox" type="number" style="width:120px">
	  <label style="margin-left:18px">客户姓名：</label><input id="customerName" class="easyui-textbox" style="width:110px" readonly="readonly"><br />
	  <label style="margin-left:18px">合同本金：</label><input id="principal" class="easyui-textbox" style="width:110px" readonly="readonly">
	  <label style="margin-left:18px">服务费率：</label><input id="serFee" class="easyui-textbox" style="width:110px" readonly="readonly">%<br />
	  <label style="margin-left:18px">合同生效日期：</label><input id="appDate" class="easyui-textbox" style="width:110px" readonly="readonly">
	  <label style="margin-left:18px">合同期数：</label><input id="cyc" class="easyui-textbox" style="width:110px" readonly="readonly">
	</div>
</div>

	<table id="cc" class="easyui-datagrid" 
		url=""
		title="" style="width:98%;minWidth:1000px;" 
		rownumbers="true" pagination="true"
		pageList="[3]" method="get"
		toolbar="#tb" singleSelect="true" pageSize="3"
		fitColumns="true" showFooter="true" rowStyler="rowStyle"
		onLoadSuccess:function(data){
                $(".note").tooltip({
                    onShow: function(){
                        $(this).tooltip('tip').css({ 
                            width:'300',
                            boxShadow: '1px 1px 3px #292929'       
                                             
                        });
                    }
                });
             } >
		<thead>
			<tr style="background:#148CCA;">
				<th data-options="field:'cyc',align:'left',formatter:showTips" style="width:6%">期数</th>
				<th data-options="field:'expDate',align:'center',formatter:showTips" style="width:9%">预计还款日</th>
				<th data-options="field:'expRepayAmount',align:'center',formatter:showTips" style="width:11%">预计还款额</th>
				<th data-options="field:'actDate',align:'center',formatter:showTips" style="width:8%">实际还款日</th>
				<th data-options="field:'actRepayAmount',align:'right',formatter:formatMoney,formatter:showMoneyTips" style="width:8%">预计还款额</th>
				<th data-options="field:'principal',align:'right',formatter:formatMoney,formatter:showMoneyTips" style="width:8%">基础服务费</th>
				<th data-options="field:'interest',align:'right',formatter:formatMoney,formatter:showMoneyTips" style="width:8%">增值服务费</th>
				<th data-options="field:'surplusPrin',align:'right',formatter:formatMoney,formatter:showMoneyTips" style="width:13%">剩余基础服务费</th>
				<th data-options="field:'totalPrin',align:'right',formatter:formatMoney,formatter:showMoneyTips" style="width:13%">累计基础服务费</th>
				<th data-options="field:'totalInterest',align:'right',formatter:formatMoney,formatter:showMoneyTips" style="width:13%">累计增值服务费</th>
			</tr>
		</thead>
	</table>
	
	<div>
	  <label style="margin-left:18px">累计基础服务费：</label><input id="totalPrin" style="width:120px" readonly="readonly">
	  <label style="margin-left:18px">累计增值服务费：</label><input id="totalInterest" style="width:110px" readonly="readonly"><br />
	  <label style="margin-left:18px">累计延迟服务费：</label><input id="delayAmountSum" style="width:110px" readonly="readonly">
	  <label style="margin-left:18px"><span style="color: red;">*</span>强制结束收款金额：</label><input type="number" id="forceAmount" style="width:110px" onchange="countSum();"><br />
	  <label style="margin-left:18px">本合同累计服务费：</label><input id="totalSum" style="width:110px" readonly="readonly">
	  <label style="margin-left:18px">本合同收益：</label><input id="income" style="width:110px" readonly="readonly">
	</div>
	<div id="dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saved();">保存</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:reload(true);">返回</a>
	</div>
</div>
</body>
</html>